package com.lujieni.netty.c3;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;
import java.util.Scanner;

public class EventClient {
    public static void main(String[] args) throws InterruptedException {
        ChannelFuture cf = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                .connect(new InetSocketAddress("localhost", 8080)); //truly execute connection thread is in NioEventLoopGroup its async

        cf.sync(); //wait until connection has been established
        Channel channel = cf.channel();
        channel.writeAndFlush("hello");


      /*
        cf.addListener(new ChannelFutureListener() {
            // truly execute operationComplete method is nio thread
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Channel cl = future.channel();
                cl.writeAndFlush("hello");
            }
        });*/

    }
}
